//
//  RegisterViewController.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/28.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "RegisterViewController.h"
#import "Register.h"

@interface RegisterViewController () {
}

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"登录/注册"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];

    _verficationBtn.layer.masksToBounds = YES;
    _verficationBtn.layer.cornerRadius = BTN_RADIUS;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _telNum.text = @"";
    _verficationBtn.userInteractionEnabled = NO;
    _verficationBtn.backgroundColor = UNUSEABLECOLOR;
}

- (IBAction)verficationClick:(id)sender {
    [self.view endEditing:YES];
    JSVIEWSHOW;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/checkMobileIsReg";
    request.params = @{@"mobile":_telNumStr};
    [DZNetAPI request:request success:^(id response) {
        JSVIEWHIDE;
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 10018) {
            Register *regist = [[Register alloc] init];
            regist.telNum = _telNumStr;
            [self.navigationController pushViewController:regist animated:YES];
        }else if (model.rt == 10014){
            LoginViewController *login = [[LoginViewController alloc] init];
            login.telNum = _telNumStr;
            [self.navigationController pushViewController:login animated:YES];
        }else{
          JSSHOW(response[@"msg"]);
        }
    } failure:^(NSError *error) {
        JSVIEWHIDE;
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (![CommTools isPureNumandCharacters:string] && string.length) {
        return NO;
    }
    if (textField.text.length == 11 && string.length) {
        return NO;
    }
    if (textField.text.length == 10 && [CommTools isPureNumandCharacters:string]) {
        _telNumStr = [textField.text stringByAppendingString:string];
        _verficationBtn.backgroundColor = BLUE_COLOR;
        _verficationBtn.userInteractionEnabled = YES;
    } else {
        _verficationBtn.backgroundColor = UNUSEABLECOLOR;
        _verficationBtn.userInteractionEnabled = NO;
    }
    return YES;
}

@end
